const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:{},
    name:'',
    place:{},
    index:0,
    pname:'存放地点',
    pid:0,
    newobj:{id:0,name:'全部'},
    page:1,
    index1:0,
    cate:{},
    cateName:'资产分类',
    index2:0,
    cid:0,
    text:'上划刷新加载更多..',
    pages:0,
    username:'',
    uid:'',
    outShow:false,
    info:{},
    typeArr:['使用中', '正常', '破损','报废', '待维修','借出'],
    typeIndex:0,
    placeArr:[],
    placeIndex:0,
    form:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function() {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //wx.setStorageSync('uid',2)
    if(!wx.getStorageSync('uid')){
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      uid:wx.getStorageSync('uid'),
      username:wx.getStorageSync('username')
    })
    this.getlist();
    this.getPlace();
    this.getCate();
  },
  getPlace:function(){
    var that = this;
    wx.request({
      url: app.globalData.url+'api/place/list',
      method: 'get',
      data:{},
      success:function(res){
        that.setData({
          placeArr:res.data.data
        })
        res.data.data.unshift(that.data.newobj)
        that.setData({
          place:res.data.data
        })
      }
    })
  },
  bindPlace: function(e) {
    this.setData({
      index: e.detail.value,
      pname:this.data.place[e.detail.value]['name'],
      pid:this.data.place[e.detail.value]['id']
    })
    this.getPub();
  },
  bindCate: function(e) {
    this.setData({
      index2: e.detail.value,
      cateName:this.data.cate[e.detail.value]['name'],
      cid:this.data.cate[e.detail.value]['id']
    })
    this.getPub();
  },
  getPub:function(){
    this.setData({
      page:1,
      list:[]
    })
    this.getlist();
  },
  inputName:function(e){
    this.setData({
      name:e.detail.value
    })
    this.getPub();
  },
  getCate:function(){
    var that = this;
    wx.request({
      url: app.globalData.url+'api/cate/list',
      method: 'get',
      data:{},
      success:function(res){
        res.data.data.unshift({id:0,name:'全部'})
        that.setData({
          cate:res.data.data
        })
      }
    })
  },
  getlist:function(){
    wx.showLoading({
      title: '数据加载中。。。',
    });
    var data = {
      name:this.data.name,
      place:this.data.pid,
      page:this.data.page,
      cid:this.data.cid
    }
    var that = this;
    wx.request({
      url: app.globalData.url+'api/stock/all',
      method: 'get',
      data:data,
      success:function(res){
        var obj = res.data.data.data;
        for(var index in obj){
          if(!obj[index]['img']){
            obj[index]['img'] = '../../img/tupian.png'
          }else{
            obj[index]['img'] = app.globalData.url+obj[index]['img']
          }
        }
        wx.hideLoading();
        if(that.data.page !== 1){
          var obj = that.data.list.concat(obj)
        }
        that.setData({
          list:obj,
          pages:Math.ceil(res.data.data.total / res.data.data.per_page)
        })
      }
    })
  },
  goinfo:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/stock/edit?id='+id
    })
  },
  infodel:function(e){
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
            // 用户点击了确定 可以调用删除方法了
            wx.request({
              url: app.globalData.url+'api/stock/save',
              method: 'put',
              data:{id:id,status:2},
              success:function(res){
                if(res.data.code == 1){
                  wx.showToast({
                    title:'删除成功',
                  });
                  that.getlist()
                }else{
                  wx.showToast({
                    title:'删除失败',
                    icon:'none'
                  });
                }
              }
            })
          } else if (sm.cancel) {
            console.log('用户点击取消')
          }
        }
      })
  },
  stockOut:function(e){
    let info = e.currentTarget.dataset.info
    this.setData({
      outShow:true,
      info:info
    })
  },
  outClick:function(){
    this.setData({
      outShow:false
    })
  },
  bindType:function(e){
    let value = e.detail.value
    var form = this.data.form
    this.setData({
      typeIndex:value
    })
  },
  placeBind:function(e){
    let value = e.detail.value
    this.setData({
      placeIndex:value
    })
  },
  inputNum:function(e){
    var form = this.data.form
    form.num = e.detail.value
    this.setData({
      form:form
    })
  },
  okClick:function(){
    if(!this.data.form.num || this.data.form.num == 0){
      wx.showToast({
        title:'请输入出库数量',
        icon:'none'
      });
      return;
    }
    if(this.data.form.num > this.data.info.snum){
      wx.showToast({
        title:'库存不足',
        icon:'none'
      });
      return;
    }
    var form = this.data.form
    form.stock_id = this.data.info.id
    form.place = this.data.placeIndex - 1 + 2
    form.type = this.data.typeArr[this.data.typeIndex]
    form.owner = wx.getStorageSync('username')
    var that = this
    wx.request({
      url: app.globalData.url+'api/stock/log/add',
      method: 'post',
      data:form,
      success:function(res){
        if(res.data.code == 1){
          wx.showToast({
            title: '出库成功',
            duration:2000,
            success:function(){
              setTimeout(function () {
                that.setData({
                  outShow:false
                })
                that.getPub();
              }, 2000) //延迟时间
            }
          })
        }else{
          wx.showToast({
            title:res.data.msg,
            icon:'none'
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (){
    if(this.data.page < this.data.pages){
      this.setData({
        page:this.data.page + 1
      })
      this.getlist();
    }else{
      this.setData({
        text:'没有更多了..'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})